package com.movieSystem.model;

import java.io.*;

public class Serial {
    /**
     * 对象序列化到字节和
     * 字节反序列化到对象
     */
    public static byte[] objectToBytes(Object o){
        ByteArrayOutputStream b=new ByteArrayOutputStream();
        try {
            ObjectOutputStream os = new ObjectOutputStream(b);
            os.writeObject(o);
            return b.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static <T> T byteToObject(byte[] bytes){
        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
        try {
            ObjectInputStream oi = new ObjectInputStream(bi);
            return (T)oi.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}
